package com.broada.apm.service.impl;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.broada.apm.ajax.Result;
import com.broada.apm.base.BaseService;
import com.broada.apm.entity.ConfigVariable;
import com.broada.apm.indicator.api.common.ApmDataIndicatorException;
import com.broada.apm.model.Analyzer;
import com.broada.apm.model.Filter;
import com.broada.apm.service.AnalyzerService;
import com.broada.apm.utils.ExceptionUtils;

public class AnalyzerServiceImpl extends BaseService implements AnalyzerService{
	
	private Logger log = LoggerFactory.getLogger(AnalyzerServiceImpl.class);
	
	@Override
	public Result getAllAnalyzer(int pageIndex, int pageSize) {
		return analyzerLogic.getAllAnalyzer(pageIndex, pageSize);
	}

	@Override
	public Result addAnalyzer(Analyzer analyzer) {
		return analyzerLogic.addAnalyzer(analyzer);
	}

	@Override
	public Result deleteAnalyzer(String id) {
		Result result = new Result();
		try {
			result = analyzerLogic.deleteAnalyzer(id);
		} catch (ApmDataIndicatorException e) {
			ExceptionUtils.error(log, "删除协议分析器数据包过滤异常", e);
			result.setFailMessage("删除协议分析器数据包过滤异常");
		}
		return result;
	}

	@Override
	public Result getFilters(String analyzerId) {
		return analyzerLogic.getFilters(analyzerId);
	}

	@Override
	public Result addDataFilter(ConfigVariable variable, String analyzerId) {
		Result result = new Result();
		try {
			result.put("order", analyzerLogic.addConfigData(variable, analyzerId));
		} catch (Exception e){
			if(e instanceof ApmDataIndicatorException){
				//数据回滚
				analyzerLogic.deleteFilters(analyzerId);
				ExceptionUtils.error(log, "设置协议分析器数据包过滤异常", e);
				result.setFailMessage("设置协议分析器数据包过滤异常");
			}else{
				ExceptionUtils.error(log, "更新协议分析器信息异常", e);
				result.setFailMessage("更新协议分析器信息异常");
			}
		}
		return result;
	}

	@Override
	public Result getAnalyzerLog(String analyzerId) {
		return analyzerLogic.getAnalyzerLog(analyzerId);
	}

	@Override
	public Result reloadProtocol(String analyzerId) {
		Result result = new Result();
		if(StringUtils.isBlank(analyzerId)){
			result.setFailMessage("请提供协议分析器标识");
			return result;
		}
		return analyzerLogic.reloadProtocol(analyzerId);
	}

	@Override
	public Result getAnalyzerById(String analyzerId) {
		Result result = new Result();
		if(StringUtils.isBlank(analyzerId)){
			result.setFailMessage("请提供协议分析器标识");
			return result;
		}
		Analyzer analyzer = analyzerLogic.getAnalyzerById(analyzerId);
		result.put("analyzer", analyzer);
		return result;
	}

	@Override
	public Result deleteDataFilter(String analyzerId, String id) {
		Result result = new Result();
		if(StringUtils.isBlank(analyzerId)){
			result.setFailMessage("请提供协议分析器标识");
			return result;
		}
		if(StringUtils.isBlank(id)){
			result.setFailMessage("请提供要删除的数据包过滤规则标识");
			return result;
		}
		try {
			result = analyzerLogic.deleteFilterById(analyzerId, id);
		} catch (Exception e) {
			if(e instanceof ApmDataIndicatorException){
				ExceptionUtils.error(log, "设置协议分析器数据包过滤异常", e);
				result.setFailMessage("设置协议分析器数据包过滤异常");
			}else{
				ExceptionUtils.error(log, "删除协议分析器过滤包信息异常", e);
				result.setFailMessage("删除协议分析器过滤包信息异常");
			}
		}
		return result;
	}

	@Override
	public Result addFilter(Filter filter) {
		Result result = new Result();
		try {
			analyzerLogic.addFilter(filter);
		} catch (Exception e){
			if(e instanceof ApmDataIndicatorException){
				ExceptionUtils.error(log, "设置协议分析器数据包过滤异常", e);
				result.setFailMessage("设置协议分析器数据包过滤异常");
			}else{
				ExceptionUtils.error(log, "配置协议分析器信息异常", e);
				result.setFailMessage("配置协议分析器信息异常");
			}
		}
		return result;
	}
}
